<?php
namespace Home\Controller;
use Think\Controller;
include_once("Common.php");
include_once("mysqlha.php");
include_once("UpFileTool.class.php");
// 用于显示主页面
class TuangouadController extends Controller
{
    
	//显示拼团专区
	public function showList(){

		$page = $_REQUEST["pageNum"];
		$pagecount = $_REQUEST['numPerPage'];
		$keywords = $_REQUEST['keywords'];
		if($page == null||$page<=1){
			$page=1;
		}

		if($pagecount == null||$pagecount<=0){
			$pagecount=20;
		}
		$Model=M();
		$startcount = ($page-1)*$pagecount;
		if($keywords == "") {
			$result = $Model->Query("select * from buygroupad order by sort  limit ".$startcount.",".$pagecount."");
			for($i=0;$i<sizeof($result);$i++){
				$name = $Model->Query("select name from buygroup where buygroup_id=".$result[$i]['buygroup_id']."");
				$result[$i]['buygroupname'] = $name[0]['name'];
				$classifys = explode(',',$result[$i]['classifys']);
                
			}
			$count = $Model->Query("select count(*) as count FROM buygroupad");
		}else{
		}
		$param = array(
			"count"=>$count[0]['count'],
			"numPerPage"=> $pagecount,
			"pageNum"=>$page
		);
		$this->assign('param',$param);
		$this->assign('resultList',$result);
		$this->display("Admin/buygroupadList.php");
	}
	
	//修改分类页面
    public function changeOne(){
        $Model = M();
        $id = $_REQUEST['id'];
		$Obj = $Model->Query("select * from buygroupad where buygroupad_id=".$id."");
		
		$classifylist = $Model->Query("select * from categroy ");
		$buygrouplist = $Model->Query("select * from buygroup order by sort");
		$classifys = explode(',',$Obj[0]['classifys']);
		
		for($i=0;$i<sizeof($classifylist);$i++){
			$classifylist[$i]['ischeck'] = false;
			for($j=0;$j<sizeof($classifys);$j++){
				if($classifys[$j] == $classifylist[$i]['cateid']){
					$classifylist[$i]['ischeck'] = true;
					break;
				}
			}
		}
		
		$this->assign('classifylist',$classifylist);
		$this->assign('buygrouplist',$buygrouplist);
		$this->assign('Obj',$Obj[0]);
		$this->assign('id',$id);
        $this->display('Admin/changbuyGroupAd.php');
    }

    //修改分类
    public function OneChange(){
        $classifys = $_REQUEST['classifys'];
		$buygorup_id = $_REQUEST['buygorup_id'];
	   
		$sort = $_REQUEST['sort'];
        $insertArr['buygroup_id'] = $buygorup_id;
		$insertArr['classifys'] = implode(',',$classifys);
		$id = $_REQUEST['id'];
		$insertArr['sort'] = $sort;
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('images');// 实例化上传类

            $UpFileTool->upImage(0.3,'Uploads','Uploads');
            $images=$UpFileTool->srcImage;
            $sumbimages = $UpFileTool->sumbImage;
            if($images != ""){
				$insertArr['sumbimage'] = $images;
				$insertArr['image'] = $sumbimages;
			}
        }
        
		update('buygroupad',$insertArr,"buygroup_id=".$buygorup_id."");
		
        $returnArr = array(
			"code"=>200,
			"callbackType"=>"closeCurrent",
			"navTabId"=>"buygroupad_list",
			"forwardUrl"=>"index.php?c=Tuangouad&a=showList",
			"msg"=>"插入成功"
		);
		echo json_encode($returnArr);
    }
	
	public function ChangeSort(){
		$sort = $_REQUEST['sort'];
		$id = $_REQUEST['id'];

		$updateArr = array(
		    "sort"=>$sort
		);
		update('buygroup',$updateArr,"buygroup_id=".$id."");
		$returnArr = array(
			"code"=>200,
			"forwardUrl"=>'index.php?c=Tuangou&a=showList',
			"msg"=>"更新成功"
		);
		echo json_encode($returnArr);

	}
	
	//获取金钱详情
	public function getMD($money){
		$result = "";
		if($money>=0&&$money<100){
			$result=$money."件";
		}else if($money>=100&&$money<1000){
			$b = (int)$money/100;
			$result=$b."百件";
		}else if($money>=1000&&$money<10000){
			$b = (int)$money/1000;
			$result=$b."千件";
		}else if($money>=10000){
			$b = (int)$money/10000;
			$result=$b."万件";
		}
		return $result;
	}
  
	
	//获取拼团专区的商品
	public function getPinTuangroup(){
		$buygroup_id = $_REQUEST['buygroup_id'];
		$Model = M();
		$page = $_REQUEST['page'];
		$pagecount = $_REQUEST['pagecount'];
		$user_id = $_REQUEST['user_id'];
		
		if($page == null||$page<=0){
			$page=0;
		}

		if($pagecount == null||$pagecount<=0){
			$pagecount=6;
		}

		$startcount = $page*$pagecount;
		$onegroup = $Model->Query("select * from buygroup where buygroup_id=".$buygroup_id."");
		$result = $Model->Query("select tuancount,title,images,goodsid from goods where buygroup_id=".$buygroup_id." and istuangou=1 order by tuancount desc limit ".$startcount.",".$pagecount."");
		for($i=0;$i<sizeof($result);$i++){
			$result[$i]['mcount'] = $onegroup[0]['mcount'];
			$result[$i]['money'] = $onegroup[0]['money'];
			$result[$i]['tuancount'] = $this->getMD($result[$i]['tuancount']);
			$result[$i]['images'] = explode(',',$result[$i]['images']); 
			$result[$i]['images'] = $result[$i]['images'][0];
		}
		$user = array($result);
    	echo getSuccessJson($user);
	}
	
	//获取拼团专区列表
	public function getBuyGroup(){
		$Model = M();
		$result = $Model->Query("select * from buygroup");
		$user = array($result);
		echo getSuccessJson($user);
	}



    //添加拼团专区
    public function showaddOne(){
		$Model = M();
		$buygrouplist = $Model->Query("select * from buygroup order by sort");
		$classifylist = $Model->Query("select * from categroy ");
		$this->assign('classifylist',$classifylist);
		$this->assign('buygrouplist',$buygrouplist);
        $this->display('Admin/addbuyGroupAd.php');
    }

    //添加拼团专区
    public function addOne(){
        $classifys = $_REQUEST['classifys'];
		$buygorup_id = $_REQUEST['buygorup_id'];
	
		$sort = $_REQUEST['sort'];
        $insertArr['buygroup_id'] = $buygorup_id;
		$insertArr['classifys'] = implode(',',$classifys);
		$insertArr['sort'] = $sort;
        if ($_FILES) {

            $UpFileTool = new \Think\UpFileTool('images');// 实例化上传类

            $UpFileTool->upImage(0.3,'Uploads','Uploads');
            $images=$UpFileTool->srcImage;
            $sumbimages = $UpFileTool->sumbImage;
             
            $insertArr['sumbimage'] = $images;
			$insertArr['image'] = $sumbimages;
            
        }
        
		myinsert('buygroupad',$insertArr);
		
        $returnArr = array(
			"code"=>200,
			"callbackType"=>"closeCurrent",
			"navTabId"=>"buygroupad_list",
			"forwardUrl"=>"index.php?c=Tuangouad&a=showList",
			"msg"=>"插入成功"
		);
		echo json_encode($returnArr);
    }
	
}